package com.cet.lxf.collection.test01;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: algorithm
 * @description:
 * @author: 陈恩涛
 * @create: 2021-08-03 14:17
 **/
public class Main01 {
    public static void main(String[] args) {
        // 构造从start到end的序列：
        final int start = 10;
        final int end = 20;
        List<Integer> list = new ArrayList<>();
        for (int i = start; i <= end; i++) {
            list.add(i);
        }
        // 随机删除List中的一个元素:
        int removed = list.remove((int) (Math.random() * list.size()));
        int found = findMissingNumber(start, end, list);
        System.out.println(list.toString());
        System.out.println("missing number: " + found);
        System.out.println(removed == found ? "测试成功" : "测试失败");
    }

    private static int findMissingNumber(int start, int end, List<Integer> list) {
        int leftExpected = start;
        int rightExpected = end;
        int rightIndex = list.size() - 1;
        int leftIndex = 0;
        while (leftExpected < rightExpected) {
            if (leftExpected!= list.get(leftIndex++)) {
                return leftExpected;
            }
            if (rightExpected!= list.get(rightIndex--)) {
                return rightExpected;
            }
            leftExpected++;
            rightExpected--;
        }
        throw new RuntimeException("怎么可能！");
    }

}
